/*
 * Copyright 2018-10-14 21:39:18 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.CustomerAO;
import com.lap.scp.erp.bean.PayCount;
import com.lap.scp.erp.core.ContractManager;
import com.lap.scp.erp.core.CustomerManager;
import com.lap.scp.erp.domain.ContractDO;
import com.lap.scp.erp.domain.CustomerDO;
import com.lap.scp.erp.query.CustomerQueryVO;

/**
 * <pre>
 *   默认 客户信息，提供给公司工程、项目应用实现类，提供客户信息，提供给公司工程、项目相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-14 21:39:18
 * @since 2.0.0
 */
@Service("customerAO")
public class DefaultCustomerAO extends AbstractAO implements CustomerAO {
	/**
	 * 客户信息，提供给公司工程、项目业务操作对象
	 */
	@Resource
	private CustomerManager customerManager;

	@Resource
	private ContractManager contractManager;

	@Override
	public Resp<CustomerDO> insertCustomer(CustomerDO customerDO, BindingResult errors) {
		Resp<CustomerDO> resp = new Resp<>();
		try {
			super.validate(resp, errors, customerDO);
			if (resp.isSuccess()) {
				customerManager.insertCustomer(customerDO);
				resp.setData(customerDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(customerDO, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> deleteById(Integer customerId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = customerManager.deleteById(customerId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(customerId, e);
		}
		return resp;
	}

	@Override
	public Resp<CustomerDO> queryById(Integer customerId) {
		Resp<CustomerDO> model = new Resp<>();
		try {
			CustomerDO customerDO = customerManager.queryById(customerId);
			model.setData(customerDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(customerId, e);
		}
		return model;
	}

	@Override
	public Resp<PayCount> queryStatis(Integer customerId) {
		Resp<PayCount> model = new Resp<>();
		try {
			PayCount pc = new PayCount();

			CustomerDO customerDO = customerManager.queryById(customerId);
			pc.setCustomerDO(customerDO);

			ContractDO contractDO = contractManager.queryStatisByCustomerId(customerId);
			pc.setContractDO(contractDO);

			model.setData(pc);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(customerId, e);
		}
		return model;
	}

	@Override
	public PageResult<CustomerDO> queryPage(Page page, CustomerQueryVO queryVO) {
		PageResult<CustomerDO> pr = new PageResult<>();
		try {
			List<CustomerDO> list = customerManager.queryPage(page, queryVO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateCustomer(CustomerDO customerDO, BindingResult errors) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, errors, customerDO);
			if (resp.isSuccess()) {
				int flag = customerManager.updateCustomer(customerDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(customerDO, e);
		}
		return resp;
	}

	@Override
	public Resp<List<CustomerDO>> queryList() {
		Resp<List<CustomerDO>> resp = new Resp<>();
		try {
			List<CustomerDO> list = customerManager.queryList();
			resp.setData(list);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(e);
		}
		return resp;
	}

}
